package com.atguigu.champter7;

import com.atguigu.beans.WaterSensor;
import com.atguigu.utils.AtguiguUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;
import java.util.List;

public class Flink11_Watermark_Sideout {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",9999);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        SingleOutputStreamOperator<String> mainStream = env
                .socketTextStream("hadoop104", 9999)
                .map(line -> {
                    String[] watersensors = line.split(",");
                    return new WaterSensor(watersensors[0], Long.valueOf(watersensors[1]), Integer.valueOf(watersensors[2]));
                })
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                //最大乱序程度
                                .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                //分配水印
                                .withTimestampAssigner((element, ts) -> element.getTs())
                                //解决数据倾斜
                                .withIdleness(Duration.ofSeconds(5))//如果有个并行度长时间[自定义时间]未获取数据,将另一个并行度的水印给没有数据的并行度
                )
                .keyBy(WaterSensor::getId)
                .window(TumblingEventTimeWindows.of(Time.seconds(3)))
                .sideOutputLateData(new OutputTag<WaterSensor>("late") {
                })
                .process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                    @Override
                    public void process(String key,
                                        Context ctx,
                                        Iterable<WaterSensor> elements,
                                        Collector<String> out) throws Exception {
                        List<WaterSensor> list = AtguiguUtil.toList(elements);
                        String msg = key + " " + list + " " + ctx.window();
                        out.collect(msg);
                    }
                });
        mainStream
                .print("main");
        mainStream.getSideOutput(new OutputTag<WaterSensor>("late"){}).print("late");




        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
